<?php
session_start(); 
include "testingsocket.php";
ob_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><?=SITETITLE;?></title>
<link rel="stylesheet" href="styles/style.css" type="text/css" />
</head>
<body>
<?php

$Command = "./client/cli-exe /var/www/simpleftp/ftpwebclient/client/SFTP_CLIENT_PARAMS.config";
//exec("./client/cli-exe /var/www/simpleftp/ftpwebclient/client/SFTP_CLIENT_PARAMS.config");
//$_SESSION["pid"] = shell_exec("nohup $Command > /dev/null 2>&1 & echo $!");
//exec("ps $pid", $pState);
//$running = (count($pState) >= 2);
//echo $running;

//echo "in login function";
if ($_POST['username'] AND $_POST['pword']) {

    $filename = PATHSECUREFILE."/".PASSWORDFILE;
    $fp = fopen($filename, 'r');
	//echo "filed openend";
    $file_contents = fread($fp, filesize($filename));
    fclose($fp);
    $lines = explode ("\r\n", $file_contents);
    foreach ($lines as $line) {
		//echo "....reading file......";
        list($userame,$pasword) = explode('|', $line);
		echo $_POST['username'];
		echo $pasword == $_POST['pword'];
		$pasword = "test";
		//if ($userame == $_POST['username'] AND $pasword == "test") {
			//echo "...found user";
			echo "<p style=\"text-align:center; color:#fff;\">Signing in - please wait ........</p>";
			setcookie ("ck",COOKIEKEY, time()+3600);
			$_SESSION["userIP"] = $_SERVER['REMOTE_ADDR']; // store session data
			if($_POST['dir']){				
				$_SESSION["localDir"] = $_POST['dir']; // store session data				
			}
			if($_POST['rem_dir'])
				$_SESSION["remDir"] = $_POST['rem_dir']; // store session data	
			
			if($_POST['sftp']){
				$_SESSION["sftpServerIP"] = $_POST['sftp']; // store session data
				//TODO: add code here to connect to DNS, resolve the servername to retrieve IP and Ports
				//Now sending a connect request to the client process to connect to remote sftp
				connect($_POST['sftp']);				
			}
			else directConnect(SERVER_IP,SERVER_PORT);
			
			echo "redirecting to index "; 
			header("Refresh:1;url=index.php");
			break;
		//}
    }
}

if ($_GET['logout']) {
	setcookie ("ck",'', time());
	
	if(isset($_SESSION["localDir"]) OR isset($_SESSION["userIP"]) OR isset($_SESSION["sftpServerIP"])){
		unset($_SESSION["localDir"]);
		unset($_SESSION["userIP"]);
		unset($_SESSION["sftpServerIP"]);
	}
	
	//closeConn();
	disconnect();
	header("Refresh:1;url=login.php");
}


if ($_COOKIE['ck'] != COOKIEKEY) {

?>
		<form name="loginform" method="post" action="login.php">
		<table border="0" cellspacing="0" cellpadding="4" align="center" style="background-color:#fff;">
		  <tr>
		  	<td class="tdnoborder" colspan="2"><p style="font-weight:bold;"><?=SITETITLE;?></p></td>
		  </tr>
		  <tr>
		  	<td class="tdnoborder">Your IP</td>
		  	<td class="tdnoborder"><?=$_SERVER['REMOTE_ADDR'];?></td>
		  </tr>
		  <tr>
			<td class="tdnoborder">Username</td>
			<td class="tdnoborder">
				<input name="username" type="text" id="username" size="30">
			 </td>
		  </tr>
		  <tr>
			<td class="tdnoborder">Password</td>
			<td class="tdnoborder">
				<input name="pword" type="password" id="pword" size="30">
			  </td>
		  </tr>
		  
		   <tr>
			<td class="tdnoborder">SFTP server name</td>
			<td class="tdnoborder">
				<input name="sftp" type="text" id="sftp" size="30">
			  </td>
		  </tr>
		  
		  <tr>
			<td class="tdnoborder">Initial Local Directory</td>
			<td class="tdnoborder">
				<input name="dir" type="text" id="dir" size="50">
			  </td>
		  </tr>
		  
		  <tr>
			<td class="tdnoborder">Initial Remote Directory</td>
			<td class="tdnoborder">
				<input name="rem_dir" type="text" id="rem_dir" size="50">
			  </td>
		  </tr>
		  
		  <tr>
			<td class="tdnoborder">&nbsp;</td>
			<td class="tdnoborder">
				<input name="submit" type="submit" id="submit" value=" Login " class="buttons">
			  </td>
		  </tr>
		  
		  
		</table>
		</form>
		<script type="text/javascript">
			document.getElementById('username').focus();
		</script>
<?php
echo "<p style=\"text-align:center;\">".CREDIT."</p>";
}
?>
</body>
</html>
